package com.example.Android_test.specs;

import android.util.Log;
import com.example.Android_test.activity.Main;
import com.example.Android_test.util.TrainingUtils;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: dns
 * Date: 09.05.13
 * Time: 20:04
 * To change this template use File | Settings | File Templates.
 */
public class SetOfWordsSpec {
    private ArrayList<WordSpec> words = new ArrayList<WordSpec>();
    private HashSet<String> availableTrainings = new HashSet<String>();

    private String sowFileName;

    private String sowID;
    private String sowName;
    private String sowComment;
    private String sowHomeLanguage;
    private String sowTargetLanguage;
    private String sowIcon;

    private String sowVersion;
    private String sowCreatedDate;
    private String sowUpdatedDate;

    public SetOfWordsSpec() {
    }

    @Override
    public String toString() {
        String nl = System.getProperty("line.separator");
        String result = "";

        result += "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + nl;
        result += "<setofwords>" + nl;

        result += "<head>" + nl;
        result += "<sowName>" + getSowName() + "</sowName>" + nl;
        result += "<sowID>" + getSowID() + "</sowID>" + nl;
        result += "<sowVersion>" + getSowVersion() + "</sowVersion>" + nl;
        result += "<sowComment>" + getSowComment() + "</sowComment>" + nl;
        result += "<sowCreateddate>" + getSowCreatedDate() + "</sowCreateddate>" + nl;
        result += "<sowUpdateddate>" + getSowUpdatedDate() + "</sowUpdateddate>" + nl;

        result += "<sowIcon>" + getSowIcon() + "</sowIcon>" + nl;
        result += "<sowHomeLanguage>" + getSowHomeLanguage() + "</sowHomeLanguage>" + nl;
        result += "<sowTargetLanguage>" + getSowTargetLanguage() + "</sowTargetLanguage>" + nl;

        result += "<availabletrainings default=\"ETR|TW|PUWIC|GD|SPRINT\">" + nl;
        if (isTrainingAvailable(TrainingUtils.ID_TRAINING_ETR)) result += "<training title=\"Eng to rus\">ETR</training>" + nl;
        if (isTrainingAvailable(TrainingUtils.ID_TRAINING_TW)) result += "<training title=\"Type_Writting\">TW</training>" + nl;
        if (isTrainingAvailable(TrainingUtils.ID_TRAINING_PUWIC)) result += "<training title=\"Pick_up_word_in_context\">PUWIC</training>" + nl;
        if (isTrainingAvailable(TrainingUtils.ID_TRAINING_GD)) result += "<training title=\"Guess_Description\">GD</training>" + nl;
        if (isTrainingAvailable(TrainingUtils.ID_TRAINING_SPRINT)) result += "<training title=\"SPINT\">SPINT</training>" + nl;
        result += "</availabletrainings>" + nl;

        result += "</head>" + nl;

        result += "<words>" + nl;
        for (WordSpec wordSpec : this.getWords()) {
            result += wordSpec.toString();
        }
        result += "</words>" + nl;
        result += "</setofwords>" + nl;

        //Log.i(Main.LOG_TAG, result);
        return result;
    }

    public boolean isThisSetOfWordsCorrect() {
        if (getSowName().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowName()");
            return false;
        }
        if (getSowID().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowID()");
            return false;
        }
        if (getSowVersion().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowVersion()");
            return false;
        }
        if (getSowComment().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowComment()");
            return false;
        }
        if (getSowCreatedDate().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowCreatedDate()");
            return false;
        }
        if (getSowUpdatedDate().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowUpdatedDate()");
            return false;
        }
        if (getSowIcon().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowIcon()");
            return false;
        }
        if (getSowHomeLanguage().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowHomeLanguage()");
            return false;
        }
        if (getSowTargetLanguage().isEmpty()) {
            Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. getSowTargetLanguage()");
            return false;
        }

        for (WordSpec currentWord: getWords()) {
            if (currentWord.getWord().isEmpty()) {
                Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getWord()");
                return false;
            }
            if (currentWord.getTranscription().isEmpty()) {
                Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getTranscription()");
                return false;
            }

            if (isTrainingAvailable(TrainingUtils.ID_TRAINING_GD) && (currentWord.getDescription().isEmpty() ||
                    currentWord.getDescriptionTranslation().isEmpty())) {
                Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getDescription()");
                return false;
            }

            if (isTrainingAvailable(TrainingUtils.ID_TRAINING_PUWIC) &&
                    (currentWord.getInContext() == null || currentWord.getInContext().isEmpty() ||
                    currentWord.getInContextTranslation() == null || currentWord.getInContextTranslation().isEmpty())) {
                Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getInContext()");
                return false;
            }
          /*  if (currentWord.getRating() < 0) {
                Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getRating()");
                return false;
            }*/
            if (currentWord.getShowed() < 0) {
                Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getShowed()");
                return false;
            }

            for (TranslationVariantSpec translationVariant : currentWord.getTranslationVariantSpecs()) {
                if (translationVariant.getPartOfSpeech().isEmpty()) {
                    Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getPartOfSpeech()");
                    return false;
                }
                if (translationVariant.getTranslate().isEmpty()) {
                    Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getTranslate()");
                    return false;
                }
               /* if (translationVariant.getExampleOfUse() != null && translationVariant.getExampleOfUse().isEmpty()) {
                    Log.e(Main.LOG_TAG, "Set of words `" + sowFileName + "` is incorrect. currentWord.getExampleOfUse()");
                    return false;
                }*/
            }
        }

        return true;
    }

    public boolean isTrainingAvailable(String trainingName) {
        HashSet<String> stringsHashSet = getAvailableTrainings();
        for (String currentName: stringsHashSet) {
            if (currentName.equals(trainingName)) {
                return true;
            }
        }

        return false;
    }


    public HashSet<String> getAvailableTrainings() {
        return availableTrainings;
    }


    public void addAvailableTrainingName(String trainingName) {
        this.availableTrainings.add(trainingName);
    }



    public String getSowFileName() {
        return sowFileName;
    }

    public void setSowFileName(String sowFileName) {
        this.sowFileName = sowFileName;
    }

    public String getSowID() {
        return sowID;
    }

    public void setSowID(String sowID) {
        this.sowID = sowID;
    }


    public String getSowVersion() {
        return sowVersion;
    }

    public void setSowVersion(String sowVersion) {
        this.sowVersion = sowVersion;
    }

    public String getSowCreatedDate() {
        return sowCreatedDate;
    }

    public void setSowCreatedDate(String sowCreatedDate) {
        this.sowCreatedDate = sowCreatedDate;
    }

    public String getSowUpdatedDate() {
        return sowUpdatedDate;
    }

    public void setSowUpdatedDate(String sowUpdatedDate) {
        this.sowUpdatedDate = sowUpdatedDate;
    }


    public String getSowName() {
        return sowName;
    }

    public void setSowName(String sowName) {
        this.sowName = sowName;
    }

    public String getSowComment() {
        return sowComment;
    }

    public void setSowComment(String sowComment) {
        this.sowComment = sowComment;
    }


    public void addWords(ArrayList<WordSpec> words) {
        this.words = words;
    }

    public void addWord(WordSpec word) {
        this.words.add(word);
    }

    public ArrayList<WordSpec> getWords() {
        return words;
    }

    public String getSowHomeLanguage() {
        return sowHomeLanguage;
    }

    public void setSowHomeLanguage(String sowHomeLanguage) {
        this.sowHomeLanguage = sowHomeLanguage;
    }

    public String getSowTargetLanguage() {
        return sowTargetLanguage;
    }

    public void setSowTargetLanguage(String sowTargetLanguage) {
        this.sowTargetLanguage = sowTargetLanguage;
    }

    public String getSowIcon() {
        return sowIcon;
    }

    public void setSowIcon(String sowIcon) {
        this.sowIcon = sowIcon;
    }

    public boolean isGoodSOWForPUWICTraining() {
        for (WordSpec currentWord: getWords()) {
            if (currentWord.getInContext() == null || currentWord.getInContextTranslation() == null ||
                    currentWord.getInContext().isEmpty() || currentWord.getInContextTranslation().isEmpty()) {
                return false;
            }
        }
        return true;
    }

    public boolean isGoodSOWForGDTraining() {
        for (WordSpec currentWord: getWords()) {
            if (currentWord.getDescription() == null || currentWord.getDescriptionTranslation() == null ||
                    currentWord.getDescription().isEmpty() || currentWord.getDescriptionTranslation().isEmpty()) {
                return false;
            }
        }
        return true;
    }
}
